package com.kxmall.huishou.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kxmall.common.exception.ServiceException;
import com.kxmall.common.utils.StringUtils;
import com.kxmall.huishou.domain.HsWasteActivity;
import com.kxmall.huishou.domain.bo.HsWasteActivityBo;
import com.kxmall.huishou.domain.vo.HsWasteActivityVo;
import com.kxmall.huishou.mapper.HsWasteActivityMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.kxmall.huishou.domain.bo.HsActivityJoinBo;
import com.kxmall.huishou.domain.vo.HsActivityJoinVo;
import com.kxmall.huishou.domain.HsActivityJoin;
import com.kxmall.huishou.mapper.HsActivityJoinMapper;
import com.kxmall.huishou.service.IHsActivityJoinService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 活动报名记录Service业务层处理
 *
 * @author kxmall
 * @date 2023-11-23
 */
@RequiredArgsConstructor
@Service
public class HsActivityJoinServiceImpl implements IHsActivityJoinService {

    private final HsActivityJoinMapper baseMapper;
    private final HsWasteActivityMapper activityMapper;

    /**
     * 查询活动报名记录
     */
    @Override
    public HsActivityJoinVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询活动报名记录列表
     */
    @Override
    public TableDataInfo<HsActivityJoinVo> queryPageList(HsActivityJoinBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HsActivityJoin> lqw = buildQueryWrapper(bo);
        Page<HsActivityJoinVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询活动报名记录列表
     */
    @Override
    public List<HsActivityJoinVo> queryList(HsActivityJoinBo bo) {
        LambdaQueryWrapper<HsActivityJoin> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HsActivityJoin> buildQueryWrapper(HsActivityJoinBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HsActivityJoin> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getActivityId() != null, HsActivityJoin::getActivityId, bo.getActivityId());
        lqw.eq(bo.getUserId() != null, HsActivityJoin::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), HsActivityJoin::getUserName, bo.getUserName());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), HsActivityJoin::getPhone, bo.getPhone());
        return lqw;
    }

    /**
     * 新增活动报名记录
     */
    @Override
    public Boolean insertByBo(HsActivityJoinBo bo) {
        HsWasteActivityVo hsWasteActivityVo = activityMapper.selectVoById(bo.getActivityId());
        if(hsWasteActivityVo == null || hsWasteActivityVo.getStatus() == 1){
            return false;
        }

        HsActivityJoin add = BeanUtil.toBean(bo, HsActivityJoin.class);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {

            HsWasteActivity hsWasteActivity = new HsWasteActivity();
            hsWasteActivity.setId(hsWasteActivityVo.getId());
            hsWasteActivity.setJoinNum(hsWasteActivityVo.getJoinNum()+1);

            activityMapper.update(hsWasteActivity, new QueryWrapper<HsWasteActivity>().eq("id",hsWasteActivityVo.getId()));

        }
        return flag;
    }

    /**
     * 修改活动报名记录
     */
    @Override
    public Boolean updateByBo(HsActivityJoinBo bo) {
        HsActivityJoin update = BeanUtil.toBean(bo, HsActivityJoin.class);
        validEntityBeforeSave(bo);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HsActivityJoinBo bo){
        //TODO 做一些数据校验,如唯一约束
        LambdaQueryWrapper<HsActivityJoin> lqw = buildQueryWrapper(bo);
        List<HsActivityJoinVo> list =baseMapper.selectVoList(lqw);
        if (list.size() >0 ) {
            throw new ServiceException("已参加，请勿重复报名",500);
        }

    }



    /**
     * 批量删除活动报名记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }



}
